var express = require('express');
var router = express.Router();

var UserToken = require('../db/user_tokens.js');
var addressDao = require('../dao/address.js');
var addressRoutes = module.exports;

/**
 * 添加地址或编辑地址
 * @param  token
 * @param  province
 * @param  city
 * @param  street
 * @param  detail
 * @param  post_code
 * @param  name
 * @param  tel
 * @param  is_default
 * @param  id  (编辑需要传入)
 *         url/address/save
 */
addressRoutes.save = function(req, res) {
    var par = req.body;
    if(!par.token) return res.json(new ERR('token不为空！', 400));
    UserToken.checkAllLogin(par.token, null, function (err, errCode, userInfo) {
        if (err) {
            return res.json(new ERR(err, 400));
        }
        par.uid = userInfo.uid;
        par.account = userInfo.account;
        if(!par.province) return res.json(new ERR('省份不为空！', 400));
        if(!par.city) return res.json(new ERR('市区不为空！', 400));
        if(!par.street) return res.json(new ERR('街道不为空！', 400));
        if(!par.detail) return res.json(new ERR('详细地址不为空！', 400));
        if(!par.post_code) return res.json(new ERR('邮编不为空！', 400));
        if(!par.name) return res.json(new ERR('姓名不为空！', 400));
        if(!par.tel) return res.json(new ERR('电话不为空！', 400));
        if(!par.is_default) par.is_default = 0;
        delete par.token;
        addressDao.save(par, function(err, data) {
            if(err) {
                res.json(new ERR(err, 400));
            } else {
                res.json(new PKG(data));
            }
        });
    });
}
router.post('/save', addressRoutes.save);

/**
 * 地址列表
 * @param  token
 * @param  size
 * @param  page
 *         url/address/list
 */
addressRoutes.list = function(req, res) {
    var par = req.body;
    if(!par.token) return res.json(new ERR('token不为空！', 400));
    UserToken.checkAllLogin(par.token, null, function (err, errCode, userInfo) {
        if (err) {
            return res.json(new ERR(err, 400));
        }
        if(!par.size) par.size = 10;
        par.size = parseInt(par.size);
        if(!par.page) {
            par.start = 0;
        } else {
            par.start = (parseInt(par.page) - 1) * parseInt(par.size);
        }
        par.uid = userInfo.uid;
        addressDao.list(par, function(err, data){
            if(err) {
                res.json(new ERR(err, 400));
            } else {
                res.json(new PKG(data));
            }
        });
    });

}
router.post('/list', addressRoutes.list);

/**
 * 删除地址
 * @param  token
 * @param  id
 *         url/address/delete
 */
addressRoutes.delete = function(req, res) {
    var par = req.body;
    if(!par.token) return res.json(new ERR('token不为空！', 400));
    UserToken.checkAllLogin(par.token, null, function (err, errCode, userInfo) {
        if (err) {
            return res.json(new ERR(err, 400));
        }
        if(!par.id) return res.json(new ERR('id不为空！', 400));
        par.uid = userInfo.uid;
        addressDao.delete(par, function(err, data){
            if(err) {
                res.json(new ERR(err, 400));
            } else {
                res.json(new PKG(data));
            }
        });
    });
}
router.post('/delete', addressRoutes.delete);

module.exports = router;